package com.qf.blog.dao.daoimpl;

import com.qf.blog.core.utils.DruidTool2;
import com.qf.blog.dao.UserDao;
import com.qf.blog.entity.UserEntity;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.util.List;

/**
 * @author zyt
 * @version 1.0
 */
public class UserDaoImpl implements UserDao {

    // 创建一个QueryRunner对象
    QueryRunner qr = new QueryRunner(DruidTool2.dataSource);

    /**
     * 向表中插入数据
     * @param userEntity
     * @return
     * @throws Exception
     */
    @Override
    public Integer insert(UserEntity userEntity) throws Exception {
        // 先准备sql语句
            String sql = "insert into t_user(username,password,email,type,status,activation_code,create_time) values(?,?,?,?,?,?,?)";

        return qr.update(sql,userEntity.getUsername(), userEntity.getPassword(), userEntity.getEmail(), userEntity.getType(), userEntity.getStatus(), userEntity.getActivationCode(), userEntity.getCreateTime());

    }

    @Override
    public UserEntity selectByid(Integer id) throws Exception {
        return null;
    }


    @Override
    public Integer updeteByid(UserEntity userEntity) throws Exception {
        return null;
    }



    @Override
    public Integer deleteByid(Integer id) throws Exception {
        return null;
    }

    @Override
    public List selectList() throws Exception {
        return null;
    }

    @Override
    public Integer batchRemove(List ids) throws Exception {
        return null;
    }

    /**
     * 根据用户id查询
     * @param username
     * @return
     * @throws Exception
     */
    @Override
    public UserEntity selectUsernaem(String username) throws Exception{
        // 先准备sql语句
        String sql ="select * from t_user where username = ?";
        UserEntity query = qr.query(sql, new BeanHandler<UserEntity>(UserEntity.class), username);
        return query;
    }


    /**
     * 激活码
     * @param activationCode
     * @return
     * @throws Exception
     */
    @Override
    public UserEntity selectByActivationCode(String activationCode) throws Exception {
        String sql = "select * from t_user where activation_code = ?";
        return qr.query(sql, new BeanHandler<UserEntity>(UserEntity.class), activationCode);
    }
}
